<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 卡片区 -->
      <el-card shadow="never">
        <!-- 面包屑导航  -->
        <el-breadcrumb
          separator-class="el-icon-arrow-right"
          class="breadcrumb"
          v-if="id"
        >
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ $route.query.name }}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- /面包屑导航 -->
        <div class="main">
          <!-- 头部搜索区 -->
          <el-form :inline="true" ref="tagRef">
            <el-form-item label="标签名称">
              <el-input v-model="tagName"></el-input>
            </el-form-item>
            <el-form-item label="状态" style="margin-left: 30px" prop="state">
              <el-select v-model="state" :placeholder="$t('table.select')">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="mini" class="query" @click="clearQuery"
                >清除</el-button
              >
              <el-button
                type="primary"
                size="mini"
                class="query"
                @click="searchTags"
                >搜索</el-button
              >
            </el-form-item>
            <div style="float:right;">
              <el-button
                type="text"
                icon="el-icon-back"
                @click="$router.push('list')"
                style="margin-right: 10px;"
                v-if="id"
              >
                返回学科
              </el-button>
              <el-button
                type="success"
                icon="el-icon-edit"
                @click="showAddDialog('新增', null)"
                >新增标签</el-button
              >
            </div>
          </el-form>
          <!-- /头部搜索区 -->
          <el-alert
            :title="'数据一共' + total + '条'"
            type="info"
            show-icon
            :closable="false"
          >
          </el-alert>
          <!-- 列表区 -->
          <el-table :data="tagList" stripe class="tableBox" style="width: 100%">
            <el-table-column label="序号" type="index" width="80">
            </el-table-column>
            <el-table-column prop="subjectName" label="所属学科">
            </el-table-column>
            <el-table-column prop="tagName" label="标签名称"> </el-table-column>
            <el-table-column prop="username" label="创建者"> </el-table-column>
            <el-table-column label="创建日期">
              <template slot-scope="scope">
                <span>{{ scope.row.addDate | parseTimeByString }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="state" label="状态" width="120">
              <template slot-scope="scope">
                <span>{{ scope.row.state ? '已启用' : '已禁用' }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="200">
              <template slot-scope="scope">
                <el-button type="text" @click="changeTagState(scope.row)">
                  {{ scope.row.state ? '禁用' : '启用' }}
                </el-button>
                <el-button
                  type="text"
                  :disabled="scope.row.state === 1"
                  @click="showAddDialog('修改', scope.row)"
                  >修改</el-button
                >
                <el-button
                  type="text"
                  :disabled="scope.row.state === 1"
                  @click="delTag(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <!-- /列表区 -->
          <!-- 分页 -->
          <el-pagination
            class="pagination"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="tagsForm.page"
            :page-sizes="[5, 10, 12, 15]"
            :page-size="tagsForm.pagesize"
            layout="prev, pager, next,sizes, jumper"
            :total="total"
            background
            v-if="!$route.query.id"
          >
          </el-pagination>
          <!-- /分页 -->
          <!-- 添加标签的对话框 -->
          <el-dialog
            :visible.sync="addTagDialogVisible"
            width="30%"
            class="addDialog"
            @close="editData = {}"
          >
            <span slot="title" class="title">{{ text }}标签</span>
            <tags-add
              v-if="addTagDialogVisible"
              :text="text"
              :id="id"
              :editData="editData"
              @close="closeDialog"
            >
            </tags-add>
          </el-dialog>
          <!-- /添加标签的对话框 -->
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/tags'
import TagsAdd from '../components/tags-add'
export default {
  components: {
    TagsAdd
  },
  data() {
    return {
      // 获取标签列表,需要的参数
      tagsForm: {
        page: 1,
        pagesize: 10,
        subjectID: null
      },
      // 搜索框中绑定的数据
      tagName: null,
      state: null,
      tagList: [],
      total: 0,
      addTagDialogVisible: false,
      // 对话框中文字
      text: '',
      // 需修改的标签的id
      editData: {}
    }
  },
  computed: {
    id() {
      return this.$route.query.id
    }
  },
  watch: {
    id() {
      this.loadTagsList()
      this.tagsForm.subjectID = null
    }
  },
  created() {
    this.loadTagsList()
    // 从学科跳转过来的传参
    // const id = this.$route.query.id
    if (this.id) {
      this.tagsForm.subjectID = this.id
    }
  },
  methods: {
    // 获取标签列表
    async loadTagsList() {
      // 获取标签列表
      try {
        const { data } = await list(this.tagsForm)
        this.tagList = data.items
        this.total = data.counts
      } catch (err) {
        this.$message.error('获取标签列表失败,请刷新重试')
      }
    },
    // 搜索功能
    searchTags() {
      this.tagsForm.tagName = this.tagName
      this.tagsForm.state = this.state
      this.loadTagsList()
    },
    // 分页功能
    handleSizeChange(val) {
      this.tagsForm.pagesize = val
      this.loadTagsList()
    },
    handleCurrentChange(val) {
      this.tagsForm.page = val
      this.loadTagsList()
    },
    // 添加标签对话框显示
    showAddDialog(txt, data) {
      this.addTagDialogVisible = true
      this.text = txt
      // 修改情况下,将原数据显示到对话框
      if (data) {
        this.editData = data
      }
    },
    // 更改标签状态
    async changeTagState(data) {
      try {
        // 需要将要改的状态发过去
        data.state = Number(!data.state)
        const res = await changeState(data)
        this.$message.success('更改状态成功')
      } catch (err) {
        this.$message.error('更改状态失败,请重试')
      }
    },
    // 删除标签
    delTag(tag) {
      this.$confirm(`此操作将永久删除标签${tag.tagName}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove(tag)
          this.loadTagsList()
          this.$message({
            type: 'success',
            message: `删除标签${tag.tagName}成功!`
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 清除查询列表
    clearQuery() {
      this.tagName =''
      this.state = null // 注意这里必须用null, 如果用空字符串,则搜索时,按禁用的状态筛选
      this.tagsForm.tagName = null
      this.tagsForm.state = null
      this.loadTagsList()
    },
    // 关闭对话框
    closeDialog() {
      this.addTagDialogVisible = false
      this.loadTagsList()
    }
  }
}
</script>

<style scoped lang="scss">
.dashboard-container {
  ::v-deep .el-card__body {
    padding: 0;
  }
  .breadcrumb {
    padding: 20px 20px;
    border-bottom: 1px solid #ebeef5;
  }
  .main {
    padding: 20px;
  }
  .query {
    height: 36px;
    line-height: 18px;
    vertical-align: middle;
  }
  .tableBox {
    margin-top: 15px;
  }
  .pagination {
    float: right;
    margin: 20px 0;
  }
  .addDialog {
    .title {
      color: #fff;
    }
  }
}
</style>
